home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / addMenuItemSafe.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  3.5 KB  |  99 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17.  
  18. //
  19. // Safe way to add menu items from a plug-in. This method will test if 
  20. // the menu is already built or not, and either create the item right
  21. // away, or add the command to the menu construction callback.
  22. //
  23. // $mainMenu is one of the top level menus.
  24. // $cmd is the mel script to be executed to build the item. It must
  25. //        return the mel procedure to remove the added items.
  26. // $globVarName must be a global variable name, variable must be
  27. //        initialized to "". This variable is used to keep track of the
  28. //        built or registered menus.
  29. //
  30. // Example:
  31. //         global string $myGlobVariable = "";
  32. //         global string $gModelingMenus[];
  33. //         string $menu = $gModelingMenus[4]; // Edit polygon menu
  34. //         addMenuItemSafe($menu, "addMyItem", "myGlobVariable");
  35. //
  36. // global proc string addMyItem() 
  37. // {
  38. //        menuItem...; 
  39. //        return "deleteUI ...";
  40. // }
  41. //
  42. ///////////////////////////////////////////////////////////////////////////////
  43. global proc addMenuItemSafe(string $mainMenu, string $cmd, string $globVarName)
  44. {
  45.     // Get the variable value
  46.     string $tmp = "global string $"+$globVarName+
  47.         ";string $temporary = $" + $globVarName;
  48.     string $val = eval($tmp);
  49.  
  50.     // Make sure menu was not already dealt with...
  51.     if ($val != "")
  52.         return;
  53.  
  54.     string $tmp = ";global string $"+$globVarName+
  55.         ";    if (($"+$globVarName+" == \"\") || ($"+
  56.         $globVarName+" == \"CB:\")) {$"+$globVarName+" = `"+$cmd+"`;}";
  57.  
  58.     if (`menu -q -ni $mainMenu`)            // Add a new entry to the menu
  59.         eval ($tmp);
  60.     else
  61.     {
  62.         // Otherwise, we have to add a call back to the menu.
  63.         string $buildMethod = `menu -q -pmc $mainMenu`;
  64.         menu -e -pmc ($buildMethod + $tmp) $mainMenu;
  65.         string $tmp2 = "global string $"+$globVarName+
  66.             ";$"+$globVarName+" = \"CB:\"";
  67.         eval ($tmp2);
  68.     }
  69.     return;
  70. }
  71.  
  72. //
  73. // Remove menu items created with addMenuItemSafe.
  74. // $mainMenu and $globVarName arguments must be the same as for
  75. //        addMenuItemSafe.
  76. //
  77. ///////////////////////////////////////////////////////////////////////////////
  78. global proc removeMenuItemSafe(string $mainMenu, string $globVarName)
  79. {
  80.     string $tmp = "global string $" + $globVarName +
  81.         ";string $temporary = $" + $globVarName;
  82.     string $val = eval($tmp);
  83.  
  84.     if ($val != "")
  85.     {
  86.         // Remove the callback if it was added. 
  87.         string $buildMethod = `menu -q -pmc $mainMenu`;
  88.         $buildMethod = `substitute (";global string \$"+$globVarName+".*`;}")
  89.             $buildMethod ""`;
  90.         menu -e -pmc $buildMethod $mainMenu;
  91.  
  92.         if ($val != "CB:") // The menu was built, remove it.
  93.             eval $val;
  94.  
  95.         $tmp = "global string $"+$globVarName+ ";$"+$globVarName+" = \"\"";
  96.         eval ($tmp);
  97.     }
  98. }
  99.